package com.quanquan.management.controller;

import DTO.PageDTO;
import base.BaseConst;
import com.quanquan.management.dto.CouponCodeDTO;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.request.CouponPushRequestBody;
import com.quanquan.management.request.CouponRecordRequestBody;
import com.quanquan.management.request.CouponRequestBody;
import com.quanquan.management.service.CouponService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

/**
 * @author gongzhou
 * @title: CouponController
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/4/115:47
 */
@Controller
@RequestMapping("/coupon")
public class CouponController extends ManagerBaseController {

    @Autowired
    private CouponService couponService;

    /**
     * 多条件查询 优惠券列表
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @param sta
     * @param hot
     * @param ckSta
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       @RequestParam(required = false) Integer hot,
                       @RequestParam(required = false) Integer ckSta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        CouponRequestBody requestBody = new CouponRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatus(sta);
        requestBody.setStatusHot(hot);
        requestBody.setCheckState(ckSta);
        PageDTO<CouponDTO> pageDTO = couponService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "coupon/list";
    }

    @RequestMapping("/search")
    @ResponseBody
    public ResultBody getPushCoupon(@RequestParam String keyword) {
        CouponRequestBody requestBody = new CouponRequestBody(0, 999, keyword, null, false);
        requestBody.setStatus(1);
        PageDTO<CouponDTO> paged = couponService.list(requestBody);
        if (null != paged) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),paged);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }

    @RequestMapping("/list/record")
    public String listCode(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        CouponRecordRequestBody requestBody = new CouponRecordRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatus(sta);
        PageDTO<CouponCodeDTO> pageDTO = couponService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "coupon/code_list";
    }

    @RequestMapping("/export/record")
    public ResultBody exportData(@RequestParam(required = false) Integer pageSize,
                                 @RequestParam(required = false) Integer page,
                                 @RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String sort,
                                 @RequestParam(required = false) String fromDate,
                                 @RequestParam(required = false) Integer total,
                                 @RequestParam(required = false) String toDate,
                                 @RequestParam(required = false) boolean isAsc) {

        CouponRecordRequestBody requestBody = new CouponRecordRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setSize(total);
        try{
            couponService.export(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }

    @RequestMapping("/send/record")
    public String sendRecord(@RequestParam(required = false) Integer pageSize,
                           @RequestParam(required = false) Integer page,
                           @RequestParam(required = false) String keyword,
                           @RequestParam(required = false) String sort,
                           @RequestParam(required = false) String fromDate,
                           @RequestParam(required = false) String toDate,
                           @RequestParam(required = false) boolean isAsc,
                           @RequestParam(required = false) Integer sta,
                           Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        CouponRecordRequestBody requestBody = new CouponRecordRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setStatus(sta);
        PageDTO<CouponCodeDTO> pageDTO = couponService.sendRecord(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "coupon/send_list";
    }

    /**
     * 添加
     * @param requestBody
     * @return
     */
    @RequestMapping("/add")
    public String add(CouponRequestBody requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "coupon/form";
        }
        boolean add = couponService.addCoupon(requestBody);
        if (add) {
            return "redirect:/coupon/list";
        } else {
            return null;
        }
    }

    /**
     * 编辑
     * @param id
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, CouponRequestBody requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            CouponDTO byId = couponService.findById(id);
            model.addAttribute("editable",byId);
            return "coupon/form";
        }
        boolean update = couponService.update(requestBody);
        if (update) {
            return "redirect:/coupon/list";
        } else {
            return null;
        }
    }


    @PostMapping("/push")
    @ResponseBody
    public ResultBody push(@RequestBody CouponPushRequestBody requestBody) {
        return couponService.push(requestBody);
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResultBody updateStatus(@RequestParam String id, @RequestParam Integer status) {
        CouponRequestBody requestBody = new CouponRequestBody();
        requestBody.setId(id);
        requestBody.setStatus(status);
        Integer changeStatus = null;
        if (status == 1) {
            changeStatus = 0;
        }
        if (status == 0) {
            changeStatus = 1;
        }
        boolean b = couponService.updateStatus(requestBody);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),changeStatus);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 导出发放记录
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @return
     */
    @RequestMapping("/export/send_record")
    public ResultBody exportSendRecord(@RequestParam(required = false) Integer pageSize,
                                 @RequestParam(required = false) Integer page,
                                 @RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String sort,
                                 @RequestParam(required = false) String fromDate,
                                 @RequestParam(required = false) String toDate,
                                 @RequestParam(required = false) Integer total,
                                 @RequestParam(required = false) boolean isAsc) {

        CouponRecordRequestBody requestBody = new CouponRecordRequestBody(page, pageSize, keyword, sort, isAsc);
        requestBody.setSize(total);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        try{
            couponService.exportSendRecord(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }
}
